package com.epoch.servicecourse.service.impl;

import com.epoch.servicecourse.bean.Chapter;
import com.epoch.servicecourse.bean.Point;
import com.epoch.servicecourse.bean.Vocabulary;
import com.epoch.servicecourse.dao.ChapterDao;
import com.epoch.servicecourse.service.ChapterService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Objects;

/**
 * @author mi
 */
@Service
@Slf4j
public class ChapterServiceImpl implements ChapterService {

    @Resource
    private ChapterDao chapterDao;
    @Override
    public List<Object> getChapterBy(String courseId) {
        List<Chapter> chapterList = chapterDao.getChapterBy(courseId);
        List<Object> objectsList = new ArrayList<>();
        List<Object> resultList = new ArrayList<>();
        int i=0;
        for(Chapter list:chapterList){
            HashMap<String,Object> chapterMap = new HashMap<>(10);
            HashMap<String,Object> map = new HashMap<>(10);
            Object[] arrayList = new Object[objectsList.size()];
            objectsList.toArray(arrayList);
            if(!Objects.equals(chapterList.get(i).getChapterId(), list.getChapterId())){
                chapterMap.put("subs",arrayList);
                chapterMap.put("chapterId",chapterList.get(i).getChapterId());
                chapterMap.put("chapterName",chapterList.get(i).getChapterName());
                resultList.add(chapterMap);
                objectsList.clear();
            }
            map.put("chapterIdIn",list.getChapterIdIn());
            map.put("chapterIdInName",list.getChapterIdInName());
            map.put("videoPath",list.getVideo().getVideoPath());
            map.put("videoId",list.getVideoId());
            map.put("point",list.getPoint());
            objectsList.add(map);
            i=chapterList.indexOf(list);
            if(chapterList.indexOf(list)==chapterList.size()-1){
                chapterMap.put("subs",objectsList);
                chapterMap.put("chapterId",chapterList.get(i).getChapterId());
                chapterMap.put("chapterName",chapterList.get(i).getChapterName());
                resultList.add(chapterMap);
            }
        }
        return resultList;
    }

    @Override
    public List<Object> getPointBy(String courseId, String chapterId) {
        List<Point> pointList = chapterDao.getPointBy(courseId,chapterId);
        List<Object> resultList = new ArrayList<>();
        List<Object> objectsList = new ArrayList<>();
        for(Point list:pointList){
            HashMap<String,Object> chapterMap = new HashMap<>(10);
            List<Vocabulary> vocabularys=chapterDao.getVocabularyBy(list.getPointId());
            for(Vocabulary lists:vocabularys){
                HashMap<String,Object> map = new HashMap<>(10);
                map.put("point",lists.getPoint());
                map.put("sentence",lists.getSentence());
                map.put("trans",lists.getTrans());
                objectsList.add(map);
            }
            Object[] arrayList = new Object[objectsList.size()];
            objectsList.toArray(arrayList);
            chapterMap.put("subs",arrayList);
            chapterMap.put("vocabulary",list.getVocabulary());
            chapterMap.put("means",list.getMeans());
            if(list.getLevel()==0){
                chapterMap.put("level","低等");
            }else if(list.getLevel()==1){
                chapterMap.put("level","中等");
            }else {
                chapterMap.put("level","高等");
            }
            chapterMap.put("record",list.getRecord());
            resultList.add(chapterMap);
            objectsList.clear();
        }
        return resultList;
    }
}
